import store from "@/store/index"

const baseUrl = "http://hbt123.xyz:8082"
const webSocketUrl="ws://hbt123.xyz:8082"
const get = "GET"
const post = "POST"
const MiniApp = window.MiniApp

function request(data, path, method) {
  return new Promise((resolve, reject) => {
    MiniApp.request({
      url: baseUrl +"/"+ path,
      data: data,
      dataType: 'text',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded' // 默认值
      },
      method: method,
      success(res) {
        resolve(res)
      },
      fail(err) {
        reject(err)
      }
    })
  })
}

const cancelCollect=(data)=>request(data,"dis_like",post)

const deleteHistory=(data)=>request(data,"del_his",post)

const deleteFriend=(data)=>request(data,"del_friend",post)

const uploadUserInfo = (data) => request(data, "upload_user_info", post)

const getCurListen = (data) => request(data, "get_cur_listen", post)

const playMusic = (data) => request(data, "play_music", post)

const collect = (data) => request(data, "like", post)

const exit = (data) => request(data, "out", post)

const globalConnection=(user_id)=>new Promise((resolve, reject) => {
  let socket =new WebSocket(webSocketUrl+"/global_socket/"+user_id)
  console.log("socket ready state:",socket.readyState)
  console.log(socket.send("你好"))
  socket.onopen=(res)=>{
    console.log("socket open:",res)
  }
  socket.onclose=(res)=>{
    console.log("socket close:",res)
  }
  socket.onerror=(res)=>{
    console.log("socket error:",res)
  }
  socket.onmessage=(res)=>{
    console.log("socket message:",res)
    console.debug("socket message:",res)
  }
  console.debug("global socket method end")
})

const getRoomInfo=(data) => request(data, "get_room_user_info", post)

const sendMessage=(data)=>{
  //TODO
}

const addFriend=(data) => request(data, "friend", post)



const getOutlineMatch=(data) => request(data, "get_outline_userinfo", post)

const sendMail=(data) => request(data, "send_mail", post)

const getFriends=(data) => request(data, "get_friend", post)

const getMails = (data) => request(data, "get_mails", post)

const getLikeList = (data) => request(data, "get_like", post)

const getHistoryMusic = (data) => request(data, "get_his_music", post)



const requests = {
  uploadUserInfo,
  getCurListen,
  getMails,
  collect,
  getLikeList,
  getFriends,
  getHistoryMusic,
  playMusic,
  exit,
  getRoomInfo,
  sendMessage,
  addFriend,
  getOutlineMatch,
  sendMail,
  cancelCollect,
  deleteFriend,
  deleteHistory,
  globalConnection
}
export default requests;